package com.example.test2

import scala.io.StdIn
import scala.util.Random

trait Scores {
  var wineCount:Int = _
  var loseCount:Int = _
  var drawCount:Int = _
}

class User extends Scores {
  var name: String = _
  var score: Int = _
}

class Computer extends Scores{
  var name: String = _
  var score: Int = 0
  def showFist(): Int = {
    Random.nextInt(3)
  }
}

class FistGame {
  //定义角色和出拳的两个Map以便后续不需要再进行判断
  private val FIST_MAP = Map(0 -> "剪刀", 1 -> "石头", 2 -> "布")
  private val ROLE_MAP = Map(1 -> "刘备", 2 -> "关羽", 3 -> "张飞")

  //定义两个标志位
  private var isEndGame: Boolean = false
  private var isBreak: Boolean = false

  var computer: Computer = _
  var user: User = _
  private var gameCount = 0

  def start(): Unit = {
    var flag = true
    while (!isBreak) {
      println("请选择对战角色：1. 刘备\t2. 关羽\t3. 张飞")
      val enemy: Int = StdIn.readInt()
      if (enemy < 1 || enemy > 3)
        println("选择敌人有误")
      else {
        computer = new Computer()
        computer.name = ROLE_MAP.get(enemy).get
        println(s"你选择了与${computer.name}对战")
        println("要开始吗？y/n")
        val char = StdIn.readChar()
        char match {
          case 'y' => fistWithCom
          case 'n' => isBreak = true
        }
      }

    }

  }

  def fistWithCom(): Unit = {
    while (!isEndGame) {
      println("请出拳！0.剪刀 1.石头 2.布")
      val userFist = StdIn.readInt()
      val comFist = computer.showFist()
      println(s"你出了${FIST_MAP.get(userFist).get}")
      println(s"${computer.name}出了${FIST_MAP.get(comFist).get}")
      jundeAndRecordScore(userFist,comFist)
      println("是否开启下一轮? y/n")
      val char = StdIn.readChar()
      if(char == 'n') {
        isEndGame = true
        showresult()
        println("是否继续选择另外的角色进行对战？y/n")
        val char2 = StdIn.readChar()
        if(char2 == 'n'){
          isBreak = true
        }
      }
    }
  }

  def jundeAndRecordScore(userFist: Int, comFist: Int): Unit = {
    (userFist,comFist) match {
      case (userFist,comFist) if(userFist == comFist) => {
        user.score += 1
        computer.score += 1
        user.drawCount += 1
        computer.drawCount += 1
        println("平局，下次努力！")
      }
      case (0,2)|(1,0)|(2,1) =>{
        user.score += 2
        user.wineCount +=1
        computer.loseCount +=1
        println("恭喜，你赢了！")
      }
      case (_,_) => {
        computer.score += 2
        user.loseCount += 1
        computer.wineCount +=1
        println("很遗憾，你输了!")
      }
    }
    gameCount += 1
  }

  def showresult() = {
    println("---------------------------------------------------")
    println(s"${user.name} VS ${computer.name},共进行了${gameCount}局")
    println("姓名\t得分\t胜局\t和局\t负局")
    println(s"${user.name}\t${user.score}\t${user.wineCount}\t"+
      s"${user.drawCount}\t${user.loseCount}")

    println(s"${computer.name}\t${computer.score}\t${computer.wineCount}\t"+
      s"${computer.drawCount}\t${computer.loseCount}")

  }
}

object Main {

  def main(args: Array[String]): Unit = {
    val game = new FistGame
    game.user = new User
    game.user.name = "游客"
    game.start()
  }

}
